<template>
  <!-- eslint-disable vue/html-indent -->
  <!-- eslint-disable vue/html-self-closing -->
  <!-- eslint-disable vue/html-closing-bracket-newline -->
  <div>
    <el-card class="box-card" style="margin: 10px 0">
      <div slot="header" class="clearfix">
        <el-tabs v-model="activeName" class="tab">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            value-format="yyyy-MM-dd"
            class="data"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
          >
          </el-date-picker>
        </div>
      </div>
      <div>
        <el-row>
          <el-col :span="18">
            <div ref="charts" class="charts"></div>
          </el-col>
          <el-col :span="6">
            <h3>门店{{ title }}排名</h3>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span>肯德基</span>
                <span>987,654</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span>麦当劳</span>
                <span>876,543</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span>华莱士</span>
                <span>765,432</span>
              </li>
              <li>
                <span class="rindex w">4</span>
                <span>海底捞</span>
                <span>654,321</span>
              </li>
              <li>
                <span class="rindex w">5</span>
                <span>赛百味</span>
                <span>543,219</span>
              </li>
              <li>
                <span class="rindex w">6</span>
                <span>汉堡王</span>
                <span>432,198</span>
              </li>
              <li>
                <span class="rindex w">7</span>
                <span>真功夫</span>
                <span>321,987</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
/* eslint-disable comma-dangle */
/* eslint-disable quotes */
/* eslint-disable semi */
import * as echarts from "echarts";
import dayjs from "dayjs";
import DayOfYear from "dayjs/plugin/dayOfYear";
import { mapState } from "vuex";
export default {
  name: "Sale",
  data() {
    return {
      activeName: "sale",
      myCharts: null,
      date: [], //日历的数据
    };
  },
  computed: {
    //标题
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
    ...mapState({
      listState: (state) => {
        return state.mock.list;
      },
    }),
  },
  watch: {
    //重新修改图表配置的数据
    title() {
      this.myCharts.setOption({
        title: {
          text: `${this.title}趋势`,
        },
        xAxis: {
          data:
            this.title == "销售额"
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis,
        },
        series: [
          {
            name: "销售额",
            type: "bar",
            barWidth: "60%",
            data:
              this.title == "销售额"
                ? this.listState.userFullYear
                : this.listState.orderFullYear,
            color: "purple",
          },
        ],
      });
    },
    listState() {
      this.myCharts.setOption({
        title: {
          text: `${this.title}趋势`,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "销售额",
            type: "bar",
            barWidth: "60%",
            data: this.listState.userFullYear,
            color: "purple",
          },
        ],
      });
    },
  },
  mounted() {
    this.myCharts = echarts.init(this.$refs.charts);
    this.myCharts.setOption({
      title: {
        text: `${this.title}趋势`,
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "销售额",
          type: "bar",
          barWidth: "60%",
          data: [],
          color: "purple",
        },
      ],
    });
  },
  methods: {
    setDay() {
      const day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    setWeek() {
      const sunday = dayjs().day(0).format("YYYY-MM-DD");
      const saturday = dayjs().day(6).format("YYYY-MM-DD");
      this.date = [sunday, saturday];
    },
    setMonth() {
      const start = dayjs().date(1).format("YYYY-MM-DD");
      const end = dayjs().date(31).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    setYear() {
      dayjs.extend(DayOfYear);
      const start = dayjs().dayOfYear(1).format("YYYY-MM-DD");
      const end = dayjs().dayOfYear(365).format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
};
</script>

<style>
.el-card__header {
  border: 0;
}
</style>
<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  position: relative;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0px;
}
.right > span {
  margin: 0 15px;
}
.data {
  width: 250px;
  margin: 0 20px;
}
.charts {
  width: 100%;
  height: 300px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul > li {
  height: 8%;
  margin: 15px 0;
}
ul > li > span {
  float: left;
  line-height: 20px;
}
.rindex {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 20px;
  margin-right: 50px;
}
ul > li > span:nth-child(3) {
  float: right;
}
.w {
  background-color: white;
  color: black;
}
</style>
